package com.droidnova.android.games.vortex;

import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;
import java.nio.IntBuffer;
import java.nio.ShortBuffer;

import javax.microedition.khronos.opengles.GL10;

import android.util.Log;

/**
 * This class is an object representation of 
 * a Cube containing the vertex information,
 * color information, the vertex indices
 * and drawing functionality, which is called 
 * by the renderer.
 * 
 * @author Savas Ziplies (nea/INsanityDesign)
 */
public class cube1 {
		
	/** The buffer holding the vertices */
	private FloatBuffer vertexBuffer;
	/** The buffer holding the color values */
	private FloatBuffer colorBuffer;
	/** The buffer holding the indices */
	private ShortBuffer  indexBuffer;
	//float g=0.0f;
	//private float a1=1.0f,b1=1.0f,c1=1.0f;
	private float a1,b1,c1,incx,incy,incz,red,blue,green,alpha=0.0f;
	
	int mIndexDatatype;
	
	/** 
	 * The initial vertex definition
	 * 
	 * It defines the eight vertices a cube has
	 * based on the OpenGL coordinate system
	 */
	/*private float vertices[] = {
			            -1.0f, -1.0f, -1.0f,	//lower back left (0)
			            1.0f, -1.0f, -1.0f,		//lower back right (1)
			            1.0f,  1.0f, -1.0f,		//upper back right (2)
			            -1.0f, 1.0f, -1.0f,		//upper back left (3)
			            -1.0f, -1.0f,  1.0f,	//lower front left (4)
			            1.0f, -1.0f,  1.0f,		//lower front right (5)
			            1.0f,  1.0f,  1.0f,		//upper front right (6)
			            -1.0f,  1.0f,  1.0f		//upper front left (7)
			    							};*/
	
    /** The initial color definition */	
	/*private float colors[] = {
			            0.0f,  1.0f,  0.0f,  1.0f,
			            0.0f,  1.0f,  0.0f,  1.0f,
			            1.0f,  0.5f,  0.0f,  1.0f,
			            1.0f,  0.5f,  0.0f,  1.0f,
			            1.0f,  0.0f,  0.0f,  1.0f,
			            1.0f,  0.0f,  0.0f,  1.0f,
			            0.0f,  0.0f,  1.0f,  1.0f,
			            1.0f,  0.0f,  1.0f,  1.0f
			    								};*/
   
/*	
	private float colors[] = {
            0.0f,  0.0f,  0.0f,  1.0f,
            0.0f,  0.0f,  0.0f,  1.0f,
            0.0f,  0.0f,  0.0f,  1.0f,
            0.0f,  0.0f,  0.0f,  1.0f,
            0.0f,  0.0f,  0.0f,  1.0f,
            0.0f,  0.0f,  0.0f,  1.0f,
            0.0f,  0.0f,  0.0f,  1.0f,
            0.0f,  0.0f,  0.0f,  1.0f
    								};*/
	
	
	
    /** 
     * The initial indices definition
     * 
     * The indices define our triangles.
     * Always two define one of the six faces
     * a cube has.
     */	
	
	 
		
	/**
	 * The Cube constructor.
	 * 
	 * Initiate the buffers.
	 */
	public cube1() {
		//
	
		
	}

	/**
	 * The object own drawing function.
	 * Called from the renderer to redraw this instance
	 * with possible changes in values.
	 * 
	 * @param gl - The GL Context
	 */
	public void draw(GL10 gl, float a2,float b2,float c2,float incx1, float incy1 , float incz1,float red1,float green1,float blue1, float alpha1 ) {		
	/*	g=g2;
		a1=a2;
		b1=b2;
		c1=c2;
		float vertices[] = { 
				g, g, g,      //front bottom left  v1
				a1, g, g,     //front  bottom right  v2
				a1, b1, g,     //front  top   right   v3
				g, b1, g,     //front  top  left     v4 
				g, g, c1,    // back bottom left   v5
				a1, g, c1,    // back bottom right v6
				a1, b1, c1,    // back top right  v7
				g, b1, c1,    // back top left   v8


		};*/
		a1=a2; b1=b2; c1=c2; 
		incx = incx1 ; incy = incy1 ; incz = incz1; 
		red=red1;blue=blue1;green=green1;alpha=alpha1;
		//Log.v("values","a1="+a1+" b1="+ b1+" c1="+ c1+" incx="+ incx+" incy="+ incy+" incz="+ incz);
		//Log.v("inc"," a1+incx="+(a1+incx));
		float vertices[] = { 
				a1, b1, c1,      //front bottom left  v0
				a1+incx, b1, c1,     //front  bottom right  v1
				a1+incx, b1+incy, c1,     //front  top   right   v2
				a1, b1+incy, c1,     //front  top  left     v3 
				a1, b1, c1+incz,      //front bottom left  v4
				a1+incx,b1, c1+incz,     //front  bottom right  v5
				a1+incx, b1+incy, c1+incz,     //front  top   right   v6
				a1, b1+incy, c1+incz,     //front  top  left     v7 
						};
		float colors[] = {
				red,  green, blue,  alpha,
				red,  green, blue,  alpha,
				red,  green, blue,  alpha,
				red,  green, blue,  alpha,
				red,  green, blue,  alpha,
				red,  green, blue,  alpha,
				red,  green, blue,  alpha,
				red,  green, blue,  alpha
	    				};

		short indices[] = {
	            0, 4, 5,    0, 5, 1,
	            1, 5, 6,    1, 6, 2,
	            2, 6, 7,    2, 7, 3,
	            3, 7, 4,    3, 4, 0,
	            4, 7, 6,    4, 6, 5,
	            3, 0, 1,    3, 1, 2
							};
		ByteBuffer byteBuf = ByteBuffer.allocateDirect(vertices.length * 4);
		byteBuf.order(ByteOrder.nativeOrder());
		vertexBuffer = byteBuf.asFloatBuffer();
		vertexBuffer.clear();
		vertexBuffer.put(vertices);
		vertexBuffer.position(0);
		
		 byteBuf = ByteBuffer.allocateDirect(colors.length * 4);
		byteBuf.order(ByteOrder.nativeOrder());
		colorBuffer = byteBuf.asFloatBuffer();
		colorBuffer.clear();
		colorBuffer.put(colors);
		colorBuffer.position(0);
		
		
		
		byteBuf = ByteBuffer.allocateDirect(indices.length * 4);
		byteBuf.order(ByteOrder.nativeOrder());
		indexBuffer = byteBuf.asShortBuffer();
		indexBuffer.clear();
		indexBuffer.put(indices);
		indexBuffer.position(0);
		
		
		//Set the face rotation
		gl.glFrontFace(GL10.GL_CW);
		
		//Point to our buffers
		gl.glVertexPointer(3, GL10.GL_FLOAT, 0, vertexBuffer);
		gl.glColorPointer(4, GL10.GL_FLOAT, 0, colorBuffer);
		
		//Enable the vertex and color state
		gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
		gl.glEnableClientState(GL10.GL_COLOR_ARRAY);
		//gl.glRotatef(45.0f, 0.0f,0.0f,1.0f);
		//Draw the vertices as triangles, based on the Index Buffer information
		gl.glDrawElements(GL10.GL_TRIANGLES, 36, GL10.GL_UNSIGNED_SHORT, indexBuffer);
		gl.glDrawElements(GL10.GL_TRIANGLES, indices.length, GL10.GL_UNSIGNED_SHORT, indexBuffer);
		//gl.glColor4f(1.0f, 1.0f, 1.0f, 0.5f);	
		//gl.glDrawElements(GL10.GL_LINES, _indicesline.length, GL10.GL_UNSIGNED_SHORT, indexBuffer1);
		//Disable the client state before leaving
		gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
		gl.glDisableClientState(GL10.GL_COLOR_ARRAY);
	}
}

